package xyz.xtt.asset.entity;

import java.io.Serializable;
import java.util.Objects;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import xyz.xtt.asset.api.enums.ReferProtocolEnum;

/**
 * <p>
 * 资产扩展信息表
 * </p>
 *
 * @author author
 * @since 2024-10-31
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("tbl_asset_ext")
@ApiModel(value = "TblAssetExt对象", description = "资产扩展信息表")
public class TblAssetExt implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "ID")
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;

	@ApiModelProperty(value = "资产表ID")
	private Long assetId;

	@ApiModelProperty(value = "token_id")
	private Long tokenId;

	@ApiModelProperty(value = "链名称")
	private String chainName;

	@ApiModelProperty(value = "链ID")
	private String chainId;

	@ApiModelProperty(value = "区块号")
	private String chainBlockNum;

	@ApiModelProperty(value = "交易hash")
	private String chainTxHash;

	@ApiModelProperty(value = "上链时间")
	private Long chainTime;

	@ApiModelProperty(value = "协议标准")
	private String referProtocol;

	@ApiModelProperty(value = "预售开始")
	private Long preSellStartDatetime;

	@ApiModelProperty(value = "出售开始时间")
	private Long sellStartDatetime;

	@ApiModelProperty(value = "出售结束时间")
	private Long sellEndDatetime;

	@ApiModelProperty(value = "最新修改时间")
	private Long lastModifyDatetime;

	@ApiModelProperty(value = "资产元数据")
	private String metadata;

	@ApiModelProperty(value = "展示风格")
	private int showType;

	@ApiModelProperty(value = "是否上链，0:不上链,1:上链")
	private Integer upChain;

	@ApiModelProperty(value = "资产编号")
	@Default
	private Long seqNo = 0L;

	@ApiModelProperty(value = "公示期开始")
	private Long publicityPeriodStart;

	@ApiModelProperty(value = "公示期结束")
	private Long publicityPeriodEnd;

	@ApiModelProperty(value = "最后一次上架时间")
	private Long lastPutOnSaleDatetime;

	public ReferProtocolEnum referProtocol() {
		ReferProtocolEnum referProtocolEnum = ReferProtocolEnum.convert(referProtocol);
		return referProtocolEnum;
	}

	public Boolean isErc1155() {
		ReferProtocolEnum referProtocolEnum = ReferProtocolEnum.convert(referProtocol);
		if (Objects.isNull(referProtocolEnum)) {
			return false;
		}
		if (Objects.equals(referProtocolEnum, ReferProtocolEnum.ERC_1155)) {
			return true;
		}
		return false;
	}

	public Boolean isErc721() {
		ReferProtocolEnum referProtocolEnum = ReferProtocolEnum.convert(referProtocol);
		if (Objects.isNull(referProtocolEnum)) {
			return false;
		}
		if (Objects.equals(referProtocolEnum, ReferProtocolEnum.ERC_7211)) {
			return true;
		}
		return false;
	}

	private boolean start() {
		return Objects.nonNull(sellStartDatetime) && sellStartDatetime > 0;
	}

	private boolean end() {
		return Objects.nonNull(sellEndDatetime) && sellEndDatetime > 0;
	}

	public boolean checkUpChain() {
		return Objects.equals(1, upChain);
	}

	public boolean checkTime() {
		long curr = System.currentTimeMillis();
		if (start() && end()) {
			return curr >= sellStartDatetime && curr < sellEndDatetime;
		} else if (start() && !end()) {
			return curr >= sellStartDatetime;
		} else if (!start() && end()) {
			return curr < sellEndDatetime;
		}
		return true;
	}

	public boolean checkPublicityPeriodIsNull() {
		return (Objects.isNull(publicityPeriodStart) || Objects.equals(publicityPeriodStart, 0L))
				|| (Objects.isNull(publicityPeriodEnd) || Objects.equals(publicityPeriodEnd, 0L));
	}

	/**
	 * 公示期是否完整结束
	 * 
	 * @return
	 */
	public boolean checkPublicityPeriodEnd() {
		if (checkPublicityPeriodIsNull()) {
			return false;
		}
		long curr = System.currentTimeMillis();
		return publicityPeriodEnd > publicityPeriodStart && curr > publicityPeriodEnd;
	}

	/**
	 * 当前时间是否在公示期
	 * 
	 * @return
	 */
	public boolean checkPublicityPeriod() {
		if (checkPublicityPeriodIsNull()) {
			return false;
		}
		long curr = System.currentTimeMillis();
		return curr >= publicityPeriodStart && curr < publicityPeriodEnd;
	}

}
